package cn.com.generaldata.os.service.indicator_evaluation.industry_layout_analysis;

import org.apache.commons.lang3.Validate;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.util.BeanMapper;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 主要海洋产业区位熵评估指标计算类.
 */
public class MajorMarineIndustryLocateEntropyCalculateService extends IndicatorEvaluationCalculateService {

	private String majorMarineAddvalueIndicatorName = ProvinceIndicatorConstant.MAJOR_MARINE_ADDVALUE_NAME;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double proportion = getProportion(indicator, indicatorAccountVO);
		Double parentProportion = getParentProportion(indicator, indicatorAccountVO);

		// if (Double.valueOf(0).equals(parentProportion)) {
		// return Double.valueOf(0);
		// }
		Validate.isTrue(!(Double.valueOf(0).equals(parentProportion)));
		return proportion / parentProportion;
	}

	/**
	 * 取得主要海洋产业占比
	 * 
	 * @param indicator
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double getProportion(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		String indicatorName = ProvinceIndicatorConstant.MAJOR_MARINE_INDUSTRY_ADD_VALUE_MAP.get(indicator
				.getIndustryCode());
		Double addvalueOfIndustry = getInputOrAccountingIndicatorValue(indicatorAccountVO,
				indicatorAccountVO.getYear(), indicatorName);
		Validate.isTrue(addvalueOfIndustry != null);
		Double addvalueOfMajorMarineIndustry = getInputOrAccountingIndicatorValue(indicatorAccountVO,
				indicatorAccountVO.getYear(),
				majorMarineAddvalueIndicatorName);
		Validate.isTrue(addvalueOfMajorMarineIndustry != null);

		return addvalueOfIndustry / addvalueOfMajorMarineIndustry;
	}

	/**
	 * 取得上级主要海洋产业占比
	 * 
	 * @param indicator
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double getParentProportion(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		String indicatorName = ProvinceIndicatorConstant.MAJOR_MARINE_INDUSTRY_ADD_VALUE_MAP.get(indicator
				.getIndustryCode());
		IndicatorAccountVO vo = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
		String parentAreaId = getAreaParentId(indicatorAccountVO.getAreaId());
		vo.setAreaId(parentAreaId);

		Double addvalueOfIndustry = getInputOrAccountingIndicatorValue(vo, vo.getYear(), indicatorName);
		Validate.isTrue(addvalueOfIndustry != null);

		Double addvalueOfMajorMarineIndustry = getInputOrAccountingIndicatorValue(vo, vo.getYear(),
				majorMarineAddvalueIndicatorName);
		Validate.isTrue(addvalueOfMajorMarineIndustry != null);
		return addvalueOfIndustry / addvalueOfMajorMarineIndustry;
	}
}
